package com.yanqu.road.server.manager.activity.flowerstudy;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.flowerstudy.config.*;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.FlowerStudyBusiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.date.DateHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossFlowerStudyConfigMgr {

    private static Logger logger = LogManager.getLogger(CrossFlowerStudyConfigMgr.class.getName());

    private static Map<Integer, FlowerStudyConfig> configMap = new ConcurrentHashMap<>();

    private static RandomHelper randomHelper = new RandomHelper();

    public static synchronized void reloadConfig(){
        logger.info("start reload FlowerStudy activity config  start");
        // 获取活动
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.FlowerStudy.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        // 重新加载数据
        for (ActivityInfo item : activityInfoMap.values()) {
            reloadConfig(item);
        }
        logger.info("start reload FlowerStudy activity config end");
    }

    public static synchronized void reloadConfig(ActivityInfo activityInfo){
        List<Integer> activityIdList = new ArrayList<>();
        int activityId = activityInfo.getActivityId();
        activityIdList.add(activityId);

        //通用配置
        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(activityId);
        if(activityConfigMap == null){
            logger.error("activityConfigMap is null, activityId = " + activityId);
            return;
        }
        List<FlowerStudyTitleConfig> flowerStudyTitleConfigList = FlowerStudyBusiness.getFlowerStudyTitleConfigList(activityId);
        Map<Integer, FlowerStudyPoemConfig> flowerStudyPoemConfigMap = FlowerStudyBusiness.getFlowerStudyPoemConfigMap(activityId);
        Map<Integer, FlowerStudyVisitConfig> visitConfigMap = FlowerStudyBusiness.getFlowerStudyVisitConfigMap(activityId);
        Map<Integer, FlowerStudyExamConfig> examConfigMap = FlowerStudyBusiness.getFlowerStudyExamConfigMap(activityId);
        Map<Integer, FlowerStudyExamQuestionConfig> studyExamQuestionConfigMap = FlowerStudyBusiness.getFlowerStudyExamQuestionConfigMap(activityId);
        Map<Integer, Map<Integer, FlowerStudySessionRewardConfig>> tmpFlowerStudySessionRewardConfigMap = FlowerStudyBusiness.getFlowerStudySessionRewardConfigMap(activityId);

        //初始化
        FlowerStudyConfig config = new FlowerStudyConfig(activityInfo,visitConfigMap,flowerStudyPoemConfigMap,examConfigMap,flowerStudyTitleConfigList,studyExamQuestionConfigMap,tmpFlowerStudySessionRewardConfigMap,activityConfigMap);
        configMap.put(activityId,config);

    }

    public static FlowerStudyConfig getConfig(int activityId){
        return configMap.get(activityId);
    }

    public static List<Integer> getRandomQuestionList(int activityId,int type) {
        FlowerStudyConfig config = getConfig(activityId);
        if (config == null){
            logger.error("flowerStudy getQuestionList config is null, activityId = " + activityId);
            return null;
        }
        int needNum = 0;
        if (type == 1){
            needNum = config.getLocalExamQuestionNum();
        }else if (type == 2){
            needNum = config.getProvincialExamQuestionNum();
        }else if (type == 3){
            needNum = config.getPalaceExamQuestionNum();
        }else {
            return null;
        }

        List<Integer> questionList = new ArrayList<>(config.getStudyExamQuestionConfigMap().keySet());

        randomHelper.shuffleList(questionList);

        List<Integer> resultList = new ArrayList<>();

        if (questionList.size() < needNum){
            return null;
        }
        if (questionList.size() == needNum){
            return questionList;
        }
        for (int i = 0; i < needNum; i++) {
            resultList.add(questionList.get(i));
        }

        return resultList;
    }

    /**
     * 根据场次查看可以参加次数
     * @param activityId
     * @param type
     * @return
     */
    public static int getCanJoinTimeByType(int activityId,int type){
        FlowerStudyConfig flowerStudyConfig = configMap.get(activityId);
        if (flowerStudyConfig == null){
            logger.error("flowerStudy getCanJoinTimeByType config is null, activityId = " + activityId);
            return -1;
        }
        if (type == 1){
            return flowerStudyConfig.getLocalExamCanJoinNum();
        }else if (type == 2){
            return flowerStudyConfig.getProvincialExamCanJoinNum();
        }else if (type == 3){
            return flowerStudyConfig.getPalaceExamCanJoinNum();
        }
        return -1;
    }

    public static FlowerStudyExamQuestionConfig getExamQuestionConfig(int activityId,int questionId){
        FlowerStudyConfig flowerStudyConfig = configMap.get(activityId);
        if (flowerStudyConfig == null){
            logger.error("flowerStudy getExamQuestionConfig config is null, activityId = {},questionId={}" , activityId,questionId);
            return null;
        }
        return flowerStudyConfig.getStudyExamQuestionConfigMap().get(questionId);
    }

    public static int getOneQuestionScore(int activityId){
        FlowerStudyConfig flowerStudyConfig = configMap.get(activityId);
        if (flowerStudyConfig == null){
            logger.error("flowerStudy getOneQuestionScore config is null, activityId = " + activityId);
            return 10;
        }
        return flowerStudyConfig.getExamOneQuestionScore();
    }

    public static FlowerStudySessionRewardConfig getFlowerStudySessionRewardConfig(int activityId,int type,int rightNum){
        FlowerStudyConfig flowerStudyConfig = configMap.get(activityId);
        if (flowerStudyConfig == null){
            logger.error("flowerStudy getOneQuestionScore config is null, activityId = " + activityId);
            return null;
        }
        Map<Integer, FlowerStudySessionRewardConfig> map = flowerStudyConfig.getSessionRewardConfigMap().get(type);
        if (map == null){
            logger.error("flowerStudy getOneQuestionScore map is null, activityId = " + activityId);
            return null;
        }
        return map.get(rightNum);
    }

    public static Map<Integer,Integer> getInitJoinTimeMap(int activityId){
        FlowerStudyConfig flowerStudyConfig = configMap.get(activityId);
        if (flowerStudyConfig == null){
            logger.error("flowerStudy getJoinTime config is null, activityId = " + activityId);
            return null;
        }
        Map<Integer,Integer> map = new ConcurrentHashMap<>();
        map.put(1,flowerStudyConfig.getLocalExamCanJoinNum());
        map.put(2,flowerStudyConfig.getProvincialExamCanJoinNum());
        map.put(3,flowerStudyConfig.getPalaceExamCanJoinNum());

        return map;
    }

    public static int getRandomAiIcon(int activityId){
        FlowerStudyConfig flowerStudyConfig = configMap.get(activityId);
        if (flowerStudyConfig == null){
            logger.error("flowerStudy Icon config is null, activityId = " + activityId);
            return 106000;
        }
        return flowerStudyConfig.getRandomHead();
    }




}
